import { Response } from 'express';
import fs from 'fs-extra';
import { req_info_user_login } from '../../req_res/cla_req_info_user_login';
import { get_db_user_account } from '../../../create_acount/Func/get_db';
import {
  mdb_info_user_account,
  user_account_prop_phone,
  user_account_prop_self_account,
} from '../../../../Func/sign_info_user_account/Classes/cla_mdb_info_user_account';
import { get_file_path_cur_user_id } from '../../../../Func/Set_file/Func/get_file_path_cur_user';
import { prop_user_passworld } from '../../../create_acount/Classes/cla_req_info_create_account';
import { global_login_cur_user } from '../global_login_cur_user';
import { log_cur_user_login } from '../Log_info';
import { res_send_login_access } from '../../req_res/cla_res_send_login_access';

/**
 * 检验 账号 是否存在
 *
 * 存在 再 获取 文件信息 检验 密码 是否 相同
 *
 * 相同 true
 */
const check_user_login_account_func_obj = {
  phone: check_user_phone,
  self_account: check_user_self_account,
};

/**
 * 登录 匹配状态
 */
export enum login_status {
  /**
   * 未 匹配成功
   */
  no_match = 0,

  /**
   * 找寻到 账户
   */
  account = 1,

  /**
   * 账户 和 密码 匹配成功
   */
  account_passworld = 2,
}

type type_check_user_login_account_func_obj_key =
  keyof typeof check_user_login_account_func_obj;

/**
 * 尝试 登录 策略
 * @param req_time 请求登录 时间
 * @param res 返回 信息
 * @param account 用户 账户
 * @param passworld 用户 密码
 * @returns 尝试 登录 是否成功 登录 成功 返回 id
 */
export async function check_cur_user_account_passworld(
  cur_req_info: req_info_user_login,
  res: Response
) {
  let key: type_check_user_login_account_func_obj_key;

  let pass_status: login_status = 0;

  for (key in check_user_login_account_func_obj) {
    const check_cur_func = check_user_login_account_func_obj[key];

    /**
     * 检测 账户 密码 是否 匹配 成功
     */
    let cur_pass_status = await check_cur_func(cur_req_info, res);

    pass_status = Math.max(cur_pass_status, pass_status);

    // 账户 和 密码 匹配成功时 直接 退出
    if (pass_status == login_status.account_passworld) {
      return pass_status;
    }
  }

  return pass_status;
}

/**
 * 身份证号 登录尝试
 */
async function check_user_self_account(
  cur_info: req_info_user_login,
  res: Response
) {
  let db_user_account = get_db_user_account();

  let self_account_arr = await db_user_account
    .find({
      [user_account_prop_self_account]: cur_info.account,
    })
    .toArray();

  if (self_account_arr.length == 1) {
    let cur_user_account_info: mdb_info_user_account = <any>self_account_arr[0];

    let cur_user_id = cur_user_account_info.id;

    let cur_file_json = await fs.readJson(
      get_file_path_cur_user_id(cur_user_id)
    );

    if (cur_file_json[prop_user_passworld] == cur_info.passworld) {
      global_login_cur_user(cur_info);

      log_cur_user_login(cur_info);

      let res_send = new res_send_login_access();

      res.send(res_send);

      return login_status.account_passworld;
    }

    return login_status.account;
  }

  return login_status.no_match;
}

/**
 * 手机 号码 登录 尝试
 * @param req_time
 * @param res
 * @param account
 * @param passworld
 * @returns
 */
async function check_user_phone(cur_info: req_info_user_login, res: Response) {
  let db_user_account = get_db_user_account();

  let phone_arr = await db_user_account
    .find({
      [user_account_prop_phone]: cur_info.account,
    })
    .toArray();

  if (phone_arr.length == 1) {
    let cur_user_account_info: mdb_info_user_account = <any>phone_arr[0];

    let cur_user_id = cur_user_account_info.id;

    let cur_file_json = await fs.readJson(
      get_file_path_cur_user_id(cur_user_id)
    );

    if (cur_file_json[prop_user_passworld] == cur_info.passworld) {
      global_login_cur_user(cur_info);

      log_cur_user_login(cur_info);

      let res_send = new res_send_login_passed();

      res.send(res_send);

      return login_status.account_passworld;
    }

    return login_status.account;
  }

  return login_status.no_match;
}
